var stompClient = null;
var randomName = "user" + Math.floor(Math.random() * 1000000);//随机生成用户名
var base_url_req_public_room = "/app/chatRoom/";
var base_url_req_person_room = "/app/toUser/";
var base_url_sub_public_room = '/topic/greetings.';
var base_url_sub_person_room = '/user/topic/msg.';
var url_req_public_room, url_req_person_room,
    url_sub_public_room, url_sub_person_room;
var content = $("#input");
var list = $('#list-msg');
var msgLayout = $('#msg');
var maxMsgLength = 500;
var listUser = $('#list-user');
var userData;

function setConnected(connected) {
    $("#connect").prop("disabled", connected);
    $("#disconnect").prop("disabled", !connected);
    if (connected) {
        $("#conversation").show();
    } else {
        $("#conversation").hide();
    }
    $("#greetings").html("");
}


function connect() {
    iniUrl();
    var socket = new SockJS('http://192.168.1.10:8084/education/gs-guide-websocket');
    stompClient = Stomp.over(socket);
    stompClient.connect({username: randomName, sid: $.cookie('__cg_sid')}, function (frame) {
        setConnected(true);

        //订阅公共频道消息
        stompClient.subscribe(url_sub_public_room, function (greeting) {
            showGreeting(greeting.body);
        });

        //订阅私人消息
        stompClient.subscribe(url_sub_person_room, function (greeting) {
            showGreeting(greeting.body);
        });

        // //订阅事件消息
        // stompClient.subscribe(url_event_sub_room, function (greeting) {
        //     console.log('aaaaaaaaa');
        //     //appendUserList(greeting.body);
        // });

        //发送消息，事物开启
        var tx = stompClient.begin();
        stompClient.send(url_req_public_room, {transaction: tx.id}, JSON.stringify({
            fromUser: userInfo.nickname,
            content: 'user:' + userInfo.photo,
        }));
        tx.commit();
        // stompClient.send(url_event_req_room, JSON.stringify({
        //     fromUser: userInfo.nickname,
        //     content: userInfo.photo
        // }));

    });
}

function disconnect() {
    if (stompClient !== null) {
        stompClient.disconnect();
    }
    setConnected(false);
    console.log("Disconnected");
}

function sendMsg() {
    if (content.val() === '') {
        layer.msg('请输入要发送的内容');
        return
    }
    var msg = {'fromUser': userInfo.nickname, 'content': content.val()};
    // if ($("#toUser").val() != "") {
    //     //发送私聊
    //     url = url_req_person_room;
    //     msg.toUser = $("#toUser").val();
    // }
    stompClient.send(url_req_public_room, {}, JSON.stringify(msg));
    content.val('');
}

function iniUrl() {
    url_req_public_room = base_url_req_public_room + courseId;
    url_req_person_room = base_url_req_person_room + courseId;
    url_sub_public_room = base_url_sub_public_room + courseId;
    url_sub_person_room = base_url_sub_person_room + courseId;
    // url_event_sub_room = base_url_event_sub_room + courseId;
    // url_event_req_room = base_url_event_req_room + courseId;
}

function showGreeting(body) {
    var parse = JSON.parse(body);
    if (parse.content.indexOf('user:') !== -1) {
        var html = '<li><p class="user"><img alt="" src="' + parse.content.substring(5) + '">' + parse.fromUser + '</p></li>';
        listUser.append(html);
        // msgLayout.animate({scrollTop: msgLayout.prop('scrollHeight')}, 300);
    } else {
        if (list.find('li').length > maxMsgLength) {
            list.empty();
        }
        var li = '<li id="' + parse.id + '"><p class="user">' + parse.fromUser + '：</p><p class="msg">' + parse.content + '</p></li>';
        list.append(li);
        msgLayout.animate({scrollTop: msgLayout.prop('scrollHeight')}, 300);
    }
}
function clean() {
    $("#list-msg").html("");
}


